import socket

def getServerIP():
    ''' 获取本机ip地址 192.168.31.18'''
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]
    return "%s"%socket.gethostbyname(socket.gethostname()) # 有的计算机会返回 127.0.0.1

if __name__ == '__main__':
    print("ServerIP:", getServerIP())